﻿using IRepositoryBll;
using IRepositoryDal;
using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryBll
{
    /// <summary>
    /// 角色业务访问层
    /// </summary>
    public class RoleInfoBll : BaseDeleteBll<RoleInfo>, IRoleInfoBll
    {
        private IRoleInfoDal _roleInfoDal;
        private IR_UserInfo_RoleInfoDal _r_UserInfo_RoleInfoDal;
        private IR_UserInfo_MenuInfoDal _r_UserInfo_MenuInfoDal;

        public RoleInfoBll(IRoleInfoDal roleInfoDal, IR_UserInfo_RoleInfoDal r_UserInfo_RoleInfoDal, IR_UserInfo_MenuInfoDal r_UserInfo_MenuInfoDal):base(roleInfoDal)
        {
            _roleInfoDal = roleInfoDal;
            _r_UserInfo_RoleInfoDal = r_UserInfo_RoleInfoDal;
            _r_UserInfo_MenuInfoDal = r_UserInfo_MenuInfoDal;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roleInfo"></param>
        /// <returns></returns>
        //public bool AddRoleInfo(RoleInfo roleInfo)
        //{
        //    //RoleInfoDal roleInfoDal = new RoleInfoDal();
        //    return _roleInfoDal.Add(roleInfo);
        //}
        /// <summary>
        /// 绑定用户
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <param name="userInfoIds"></param>
        public void BindUserInfo(string roleInfoId, List<string> userInfoIds)
        {
            DateTime now = DateTime.Now;

            //当前角色已绑定的用户信息
            var userInfo_RoleInfos = _r_UserInfo_RoleInfoDal.GetEntitiesDb().Where(r => r.RoleInfoID == roleInfoId).ToList();
            //先删除已绑定的
            foreach (var item in userInfo_RoleInfos)
            {
                if (!userInfoIds.Contains(item.UserInfoID))
                {

                    _r_UserInfo_RoleInfoDal.Delete(item.ID);
                }
            }
            //添加
            foreach (var item in userInfoIds)
            {
                //如果已经存在的用户就不添加，不存在的才添加
                if (!userInfo_RoleInfos.Any(a => a.UserInfoID == item))
                {
                    R_UserInfo_RoleInfo r_UserInfo_RoleInfo = new R_UserInfo_RoleInfo()
                    {
                        ID = Guid.NewGuid().ToString(),
                        CreateTime = now,
                        RoleInfoID = roleInfoId,
                        UserInfoID = item
                    };
                    _r_UserInfo_RoleInfoDal.Add(r_UserInfo_RoleInfo);
                }
            }
        }
        /// <summary>
        /// 判断当前角色是否存在
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public bool IsHadRoleInfo(string roleName)
        {
            int count = _roleInfoDal.GetEntitiesDb().Where(r => r.RoleName == roleName && !r.IsDelete).Count();
            return count > 0;
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <returns></returns>
        //public bool DeleteRoleInfo(string roleInfoId)
        //{
        //    return _roleInfoDal.Delete(roleInfoId);
        //}

        /// <summary>
        /// 根据id获取角色
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <returns></returns>
        //public RoleInfo GetRoleInfo(string roleInfoId)
        //{
        //    return _roleInfoDal.GetEntity(roleInfoId);
        //}

        public List<RoleInfo> GetRoleInfoList()
        {
            return _roleInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false).ToList();
        }
        /// <summary>
        /// 分页、获取新增数据信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public object GetRoleInfoListByPage(int page, int limit, out int count, string roleName)
        {
            //获取数据库数据
            var roleInfo = _roleInfoDal.GetEntitiesDb().Where((u => u.IsDelete == false));
            if (!string.IsNullOrEmpty(roleName))
            {
                roleInfo = roleInfo.Where(u => u.RoleName.Contains(roleName));
            }
            //统计数据数量
            count = roleInfo.Count();
            roleInfo = roleInfo.OrderBy(r => r.CreateTime).Skip((page - 1) * limit).Take(limit);
            var list = roleInfo.ToList().Select(u =>
            {
                return new
                {
                    u.ID,
                    u.RoleName,
                    u.Description,
                    CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                };
            });

            return list;
        }



        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <returns></returns>
        //public bool SoftDeleteRoleInfo(string roleInfoId)
        //{
        //    RoleInfo roleInfo = _roleInfoDal.GetEntity(roleInfoId);
        //    if (roleInfo != null)
        //    {
        //        roleInfo.IsDelete = true;
        //        roleInfo.DeleteTime = DateTime.Now;
        //        return _roleInfoDal.Update(roleInfo);
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}



        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="roleInfo"></param>
        /// <returns></returns>
        //public bool UpdateRoleInfo(RoleInfo roleInfo)
        //{
        //    return _roleInfoDal.Update(roleInfo);
        //}
        /// <summary>
        /// 更新角色数据信息
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <param name="roleName"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public bool UpdateRoleInfo(string roleInfoId, string roleName, string description)
        {
            RoleInfo roleInfo = _roleInfoDal.GetEntity(roleInfoId);
            if (roleInfoId != null)
            {
                roleInfo.RoleName = roleName;
                roleInfo.Description = description;
                return _roleInfoDal.Update(roleInfo);
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 当前已经绑定的ID
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <returns></returns>
        public List<string> GetBindUserIds(string roleInfoId)
        {
            //查询当前角色已绑定的用户id
            return _r_UserInfo_RoleInfoDal.GetEntitiesDb().Where(r => r.RoleInfoID == roleInfoId).Select(r => r.UserInfoID).ToList();
        }


        /// <summary>
        /// 当前已经绑定的菜单ID
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <returns></returns>
        public List<string> GetBindMenuIds(string roleInfoId)
        {
            //查询当前角色已绑定的菜单id
            return _r_UserInfo_MenuInfoDal.GetEntitiesDb().Where(r => r.RoleId == roleInfoId).Select(r => r.MenuId).ToList();
        }


        /// <summary>
        /// 绑定菜单
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <param name="menuInfoIds"></param>
        public void BindMenuIds(string roleInfoId, List<string> menuInfoIds)
        {
            DateTime now = DateTime.Now;

            //当前角色已绑定的菜单信息
            var menuInfo_RoleInfos = _r_UserInfo_MenuInfoDal.GetEntitiesDb().Where(r => r.RoleId == roleInfoId).ToList();
            //先删除已绑定的
            foreach (var item in menuInfo_RoleInfos)
            {
                if (!menuInfoIds.Contains(item.MenuId))
                {

                    _r_UserInfo_MenuInfoDal.Delete(item.ID);
                }
            }
            //添加
            foreach (var item in menuInfoIds)
            {
                //如果已经存在的用户就不添加，不存在的才添加
                if (!menuInfo_RoleInfos.Any(a => a.MenuId == item))
                {
                    R_UserInfo_MenuInfo r_UserInfo_MenuInfo = new R_UserInfo_MenuInfo()
                    {
                        ID = Guid.NewGuid().ToString(),
                        CreateTime = now,
                        RoleId = roleInfoId,
                        MenuId = item
                    };
                    _r_UserInfo_MenuInfoDal.Add(r_UserInfo_MenuInfo);
                }
            }
        }
    }
}
